﻿using System;
using System.Web.Mvc;
using NOSDC.Services;
using NOSDC.Services.Extensions;
using NOSDC.Pocos;

namespace NOSDC.Areas.Admin.Controllers
{
    public class JobsController : BaseController
    {
        private readonly IJobService _service;

        public JobsController()
        {
            _service = new JobService();
        }

        public JobsController(IJobService service)
        {
            _service = service;
        }

        public ActionResult Index()
        {
            var jobs = _service.GetActiveJobs(true);

            foreach (var j in jobs)
                j.Description = j.Description.StripHTML();

            return View(jobs);
        }

        [HttpPost]
        public ActionResult Delete(int id)
        {
            try
            {
                _service.DeleteJob(id);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        public ActionResult Create()
        {
            var job = new Job
                          {
                              DatePosted = DateTime.Now
                          };

            job.DateExpires.AddDays(14);
            return View(job);
        }


        // POST: /Admin/Articles/Create
        [HttpPost, ValidateInput(false)]
        public ActionResult Create(Job model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    _service.PostJob(model);
                    return RedirectToAction("Index");
                }
                return View(model);
            }
            catch
            {
                return View(model);
            }
        }

        // GET: /Admin/Articles/Edit/5
        public ActionResult Edit(int id)
        {
            return View(_service.GetJobById(id));
        }

        // POST: /Admin/Articles/Edit/5
        [HttpPost, ValidateInput(false)]
        public ActionResult Edit(int id, Job model)
        {
            try
            {
                // TODO: Add update logic here
                model.Id = id;
                _service.UpdateJob(model);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

    }
}
